
Research
2025 Report: Destructive Malware in Open Source Packages
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.
regexparam
Advanced tools
A tiny (308B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` ๐โ
A tiny (308B) utility that converts route patterns into RegExp. Limited alternative to
path-to-regexp๐
With regexparam, you may turn a pathing string (eg, /users/:id) into a regular expression.
An object with shape of { keys, pattern } is returned, where pattern is the RegExp and keys is an array of your parameter name(s) in the order that they appeared.
Unlike path-to-regexp, this module does not create a keys dictionary, nor mutate an existing variable. Also, this only ships a parser, which only accept strings. Similarly, and most importantly, regexparam only handles basic pathing operators:
/foo, /foo/bar)/:title, /books/:title, /books/:genre/:title)/movies/:title.mp4, /movies/:title.(mp4|mov))/:title?, /books/:title?, /books/:genre/:title?)*, /books/*, /books/:genre/*)This module exposes two module definitions:
dist/regexparam.jsdist/regexparam.mjs$ npm install --save regexparam
const regexparam = require('regexparam');
// Example param-assignment
function exec(path, result) {
let i=0, out={};
let matches = result.pattern.exec(path);
while (i < result.keys.length) {
out[ result.keys[i] ] = matches[++i] || null;
}
return out;
}
// Parameter, with Optional Parameter
// ---
let foo = regexparam('/books/:genre/:title?')
// foo.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?\/?$/i
// foo.keys => ['genre', 'title']
foo.pattern.test('/books/horror'); //=> true
foo.pattern.test('/books/horror/goosebumps'); //=> true
exec('/books/horror', foo);
//=> { genre: 'horror', title: null }
exec('/books/horror/goosebumps', foo);
//=> { genre: 'horror', title: 'goosebumps' }
// Parameter, with suffix
// ---
let bar = regexparam('/movies/:title.(mp4|mov)');
// bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/?$/i
// bar.keys => ['title']
bar.pattern.test('/movies/narnia'); //=> false
bar.pattern.test('/movies/narnia.mp3'); //=> false
bar.pattern.test('/movies/narnia.mp4'); //=> true
exec('/movies/narnia.mp4', bar);
//=> { title: 'narnia' }
// Wildcard
// ---
let baz = regexparam('users/*');
// baz.pattern => /^\/users\/(.*)\/?$/i
// baz.keys => ['wild']
baz.pattern.test('/users'); //=> false
baz.pattern.test('/users/lukeed'); //=> true
exec('/users/lukeed/repos/new', baz);
//=> { wild: 'lukeed/repos/new' }
Important: When matching/testing against a generated RegExp, your path must begin with a leading slash (
"/")!
For fine-tuned control, you may pass a RegExp value directly to regexparam as its only parameter.
In these situations, regexparam does not parse nor manipulate your pattern in any way! Because of this, regexparam has no "insight" on your route, and instead trusts your input fully. In code, this means that the return value's keys is always equal to false and the pattern is identical to your input value.
This also means that you must manage and parse your own keys~!
You may use named capture groups or traverse the matched segments manually the "old-fashioned" way:
Important: Please check your target browsers' and target Node.js runtimes' support!
// Named capture group
const named = regexparam(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i);
const { groups } = named.pattern.exec('/posts/2019/05/hello-world');
console.log(groups);
//=> { year: '2019', month: '05', title: 'hello-world' }
// Widely supported / "Old-fashioned"
const named = regexparam(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i);
const [url, year, month, title] = named.pattern.exec('/posts/2019/05/hello-world');
console.log(year, month, title);
//=> 2019 05 hello-world
There are two API variants:
When passing a String input, the loose parameter is able to affect the output. View API
When passing a RegExp value, that must be regexparam's only argument.
Your pattern is saved as written, so loose is ignored entirely. View API
Returns: Object
Returns a { keys, pattern } object, where pattern is a generated RegExp instance and keys is a list of extracted parameter names.
Type: String
The route/pathing string to convert.
Note: It does not matter if your
strbegins with a/โ it will be added if missing.
Type: Boolean
Default: false
Should the RegExp match URLs that are longer than the str pattern itself?
By default, the generated RegExp will test that the URL begins and ends with the pattern.
const rgx = require('regexparam');
rgx('/users').pattern.test('/users/lukeed'); //=> false
rgx('/users', true).pattern.test('/users/lukeed'); //=> true
rgx('/users/:name').pattern.test('/users/lukeed/repos'); //=> false
rgx('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true
Returns: Object
Returns a { keys, pattern } object, where pattern is identical to your rgx and keys is false, always.
Type: RegExp
Your RegExp pattern.
Important: This pattern is used as is! No parsing or interpreting is done on your behalf.
RegExps.MIT ยฉ Luke Edwards
The path-to-regexp package is a popular utility for converting URL patterns into regular expressions. It offers similar functionality to regexparam but is more feature-rich and widely used in the community. It supports advanced pattern matching, including custom parameter types and modifiers.
The route-parser package provides a similar capability to regexparam, allowing you to define and match URL patterns. It is designed to be simple and easy to use, with a focus on readability and maintainability of route definitions.
The url-pattern package is another alternative for matching URL patterns. It offers a straightforward API for defining and matching patterns, with support for named parameters and wildcards. It is less feature-rich compared to path-to-regexp but provides a simpler interface.
FAQs
A tiny (399B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` ๐โ
The npm package regexparam receives a total of 980,569 weekly downloads. As such, regexparam popularity was classified as popular.
We found that regexparam demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.ย It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.

Security News
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.

Research
/Security News
A five-month operation turned 27 npm packages into durable hosting for browser-run lures that mimic document-sharing portals and Microsoft sign-in, targeting 25 organizations across manufacturing, industrial automation, plastics, and healthcare for credential theft.